﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hangjing.Common;
using Hangjing.SQLServerDAL;
using Hangjing.Model;
using System.Net;

using org.in2bits.MyXls; //生成excel时需要使用的引用

/// <summary>
/// 配送员结算导出
/// </summary>
public partial class Admin_Sale_SearchExcelOrder_deliversettleExcel : AdminPageBase
{
    private string SqlWhere
    {
        get
        {
            object o = ViewState["SqlWhere"];
            return (o == null) ? "" : Convert.ToString(o);
        }
        set
        {
            ViewState["SqlWhere"] = value;
        }
    }

   Custorder bll = new Custorder();

    protected void Page_Load(object sender, EventArgs e)
    {
        CheckRights("G");
        if (!IsPostBack)
        {
            if (HjNetHelper.GetQueryString("type") == "1")
            {
                //由前一个页面的检索条件直接获取Excel
                if (!string.IsNullOrEmpty(WebUtility.FixgetCookie("SearchOrderSqlWhere")))
                {
                    SqlWhere = WebUtility.FixgetCookie("SearchOrderSqlWhere");

                }
                else
                {
                    SqlWhere = "1=1";
                }
                GetExcel();
            }
        }
    }

    protected void GetExcel()
    {
        org.in2bits.MyXls.XlsDocument doc = new XlsDocument();

        //根据当前时间生成文件名 TODO;增加用户名

        doc.FileName = "OrderRecord.xls";//" + WebUtility.GetTime() + "
        doc.SummaryInformation.Author = "IHangjing"; //作者
        doc.SummaryInformation.Subject = "Order Record";
        doc.DocumentSummaryInformation.Company = "IHangjing.com";

        doc.Workbook.ProtectContents = true;

        int RowCount = bll.GetCount(SqlWhere);//TODO;改成链接查询的获取记录数目函数

        IList<CustorderInfo> list = new List<CustorderInfo>();
        list = bll.GetListFix(RowCount, 1, SqlWhere, "custorder.Unid", 1);

        string sheetName = "OrderRecord";

        Worksheet sheet = doc.Workbook.Worksheets.Add(sheetName);
        Cells cells = sheet.Cells;

        int rowMin = 1;
        int rowCount = RowCount;
        int colMin = 1;
        int colCount = 8;     //11列
        string cellName = "";  //列名
        string cellValue = ""; //列的值

        //设置列的格式
        ColumnInfo colInfo = new ColumnInfo(doc, sheet);//生成列格式对象
        colInfo.ColumnIndexStart = 0;
        colInfo.ColumnIndexEnd = 6;
        colInfo.Width = 6 * 1024;          //列的宽度计量单位为 1/256 字符宽
        sheet.AddColumnInfo(colInfo);

        ColumnInfo colInfo2 = new ColumnInfo(doc, sheet);//生成列格式对象
        //设定colInfo格式的起作用的列为第2列到第5列(列格式为0-base)
        colInfo2.ColumnIndexStart = 7;    //起始列为第二列
        colInfo2.ColumnIndexEnd = 8;      //终止列为第六列
        colInfo2.Width = 1 * 2048;          //列的宽度计量单位为 1/256 字符宽
        sheet.AddColumnInfo(colInfo2);//把格式附加到sheet页上（注：AddColumnInfo方法有点小问题，不给把colInfo对象多次附给sheet页）


        for (int row = 0; row < list.Count + 1; row++)
        {
            if (row == 0)
            {
                for (int col = 1; col <= colCount; col++)
                {

                    switch (col)
                    {
                        case 1: cellName = "订单编号"; break;
                        case 2: cellName = "时间"; break;
                        case 3: cellName = "客户姓名"; break;
                        case 4: cellName = "商家名称"; break;
                        case 5: cellName = "跑腿费用"; break;
                        case 6: cellName = "代收金额"; break;
                        case 7: cellName = "收款"; break;
                        case 8: cellName = "交款"; break;
                       
                    }
                    org.in2bits.MyXls.Cell cell = cells.Add(rowMin + row, colMin + col - 1, cellName);//列名


                }
            }
            else
            {
                for (int col = 1; col <= colCount; col++)
                {

                    switch (col)
                    {
                        case 1: cellValue = list[row - 1].orderid.ToString(); break;
                        case 2: cellValue = list[row - 1].OrderDateTime.ToString(); break;
                        case 3: cellValue = list[row - 1].OrderRcver.ToString(); break;
                        case 4: cellValue = list[row - 1].TogoName; break;
                        case 5: cellValue = list[row - 1].shopdiscountmoney.ToString(); break;
                        case 6: cellValue = list[row - 1].OrderSums.ToString(); break;
                        case 7: cellValue = list[row - 1].isauth == 1 ? "是":"否"; break;
                        case 8: cellValue = list[row - 1].isfile == 1 ? "是" : "否"; break;
                      
                    }

                    org.in2bits.MyXls.Cell cell = cells.Add(rowMin + row, colMin + col - 1, cellValue);
                }
            }

        }

        doc.Send();     //将文档发送到浏览器
        //Response.Flush();
        //Response.End();
    }

}
